const express = require("express");
const bodyParser = require("body-parser");
const db = require("./db/index");
const Students = require("./models/studentsSchema");
const app = express();
app.use(
  bodyParser.urlencoded({
    extended: true
  })
);
app.post("/stuInfo", async (req, res) => {
  await db;
  const student = req.body;
  const result = await Students.create(student);
  console.log(result);
  res.send("数据提交成功！！");
});
app.get("/stuSearch", async (req, res) => {
  await db;
  const { name } = req.query;
  const result = await Students.find({
    name
  });
  console.log(result);
  res.send("xx");
});
app.post("/stuUpdate", async (req, res) => {
  await db;
  console.log(req.body);
  const student = req.body;
  const stu_id = student.stu_id;
  const result = await Students.update({ stu_id }, { $set: { age: 20 } });
  console.log(result);
  res.send("xxxyyy");
});

app.get("/stuDel/:stu_id", async (req, res) => {
  await db;
  console.log(req.params);
  const { stu_id } = req.params;
  const result = await Students.deleteOne({ stu_id });
  console.log(result);
  res.send("删除成功！！");
});
app.listen(8080, err => {
  if (!err) console.log("server启动");
  else console.log(err);
});
